package com.haixiaoke.saas.messageCenter.service.impl;

import java.util.List;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.messageCenter.domain.PublicMessage;
import com.haixiaoke.saas.messageCenter.mapper.PublicMessageMapper;
import com.haixiaoke.saas.messageCenter.service.IPublicMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 公共消息模版信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-11-12
 */
@Service
public class PublicMessageServiceImpl implements IPublicMessageService {
    @Autowired
    private PublicMessageMapper publicMessageMapper;

    /**
     * 查询公共消息模版信息
     *
     * @param publicMessageId 公共消息模版信息主键
     * @return 公共消息模版信息
     */
    @Override
    public PublicMessage selectPublicMessageByPublicMessageId(Long publicMessageId) {
        return publicMessageMapper.selectPublicMessageByPublicMessageId(publicMessageId);
    }

    /**
     * 查询公共消息模版信息列表
     *
     * @param publicMessage 公共消息模版信息
     * @return 公共消息模版信息
     */
    @Override
    public List<PublicMessage> selectPublicMessageList(PublicMessage publicMessage) {
        return publicMessageMapper.selectPublicMessageList(publicMessage);
    }

    /**
     * 新增公共消息模版信息
     *
     * @param publicMessage 公共消息模版信息
     * @return 结果
     */
    @Override
    public int insertPublicMessage(PublicMessage publicMessage) {
        // 数据检查
        dataChecking(publicMessage);

        // 生成分组编码
        String code = "MT_" + RandomUtil.randomNumbers(7);
        publicMessage.setTemplateCode(code);
        publicMessage.setCreateTime(DateUtils.getNowDate());
        return publicMessageMapper.insertPublicMessage(publicMessage);
    }

    private static void dataChecking(PublicMessage publicMessage) {
        if (StrUtil.isBlank(publicMessage.getTemplateName()) || publicMessage.getTemplateName().length() > 20) {
            throw new ServiceException("模板名称不能为空且不能超过20个字");
        }
        if (StrUtil.isBlank(publicMessage.getMessageHeader()) || publicMessage.getMessageHeader().length() > 20) {
            throw new ServiceException("标题不能为空且不能超过20个字");
        }
        if (StrUtil.isBlank(publicMessage.getMessageContent()) || publicMessage.getMessageContent().length() > 200) {
            throw new ServiceException("消息内容不能为空且不能超过200个字");
        }
    }

    /**
     * 修改公共消息模版信息
     *
     * @param publicMessage 公共消息模版信息
     * @return 结果
     */
    @Override
    public int updatePublicMessage(PublicMessage publicMessage) {
        // 数据检查
        dataChecking(publicMessage);

        publicMessage.setUpdateTime(DateUtils.getNowDate());
        return publicMessageMapper.updatePublicMessage(publicMessage);
    }

    /**
     * 批量删除公共消息模版信息
     *
     * @param publicMessageIds 需要删除的公共消息模版信息主键
     * @return 结果
     */
    @Override
    public int deletePublicMessageByPublicMessageIds(Long[] publicMessageIds) {
        return publicMessageMapper.deletePublicMessageByPublicMessageIds(publicMessageIds);
    }

    /**
     * 删除公共消息模版信息信息
     *
     * @param publicMessageId 公共消息模版信息主键
     * @return 结果
     */
    @Override
    public int deletePublicMessageByPublicMessageId(Long publicMessageId) {
        return publicMessageMapper.deletePublicMessageByPublicMessageId(publicMessageId);
    }
}
